import { createRouter, createWebHashHistory } from 'vue-router'
import { ElMessage } from 'element-plus'
const routes = [
    {
        path: '/',
        name: 'layout',
        component: () => import('@/views/LayoutView.vue'),
        meta: {
            title: "名医在线-后台管理系统",
            isLogin: true
        },
        redirect: '/home',
        children: [
            {
                path: '/home',
                name: 'home',
                component: () => import('@/views/HomeView.vue'),
                meta: {
                    title: "名医在线-管理中心",
                    isLogin: true
                }
            },
            {
                path: '/menu',
                name: 'menu',
                component: () => import('@/views/menu/MenuIndex.vue'),
                meta: {
                    title: "名医在线-菜单管理",
                    isLogin: true
                }
            },
            {
                path: '/role',
                name: 'role',
                component: () => import('@/views/role/RoleIndex.vue'),
                meta: {
                    title: "名医在线-角色管理",
                    isLogin: true
                }
            },
            {
                path: '/admin',
                name: 'admin',
                component: () => import('@/views/admin/AdminIndex.vue'),
                meta: {
                    title: "名医在线-管理员管理",
                    isLogin: true
                }
            },
            {
                path: '/business',
                name: 'business',
                component: () => import('@/views/business/BusinessIndex.vue'),
                meta: {
                    title: "名医在线-医疗机构管理",
                    isLogin: true
                }
            },
            {
                path: '/banner',
                name: 'banner',
                component: () => import('@/views/banner/BannerIndex.vue'),
                meta: {
                    title: "名医在线-轮播图管理",
                    isLogin: true
                }
            },
            {
                path: '/staff',
                name: 'staff',
                component: () => import('@/views/staff/staffIndex.vue'),
                meta: {
                    title: "名医在线-医生管理",
                    isLogin: true
                }
            },
            {
                path: '/log',
                name: 'log',
                component: () => import('@/views/logView.vue'),
                meta: {
                    title: "名医在线-系统操作日志管理",
                    isLogin: true
                }
            },
        ]
    },
    {
        path: '/:matchPath(.*)*',
        name: '404',
        component: () => import('@/views/NotFound.vue'),
        meta: {
            title: "名医在线-404资源不存在",
            isLogin: false
        }
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('@/views/LoginView.vue'),
        meta: {
            title: "名医在线-管理员登录",
            isLogin: false
        }
    }
]

const router = createRouter({
    routes,
    history: createWebHashHistory()
})

router.beforeEach((to, from, next) => {
    if (to.meta.isLogin) {
        const { token } = JSON.parse(localStorage.getItem('adminLoginInfo') || '{}')
        if (!token) {
            ElMessage.warning('您还没有登录, 请先登录')
            return next('/login')
        }
    }
    next()
});
export default router